<?php

/*
////////////////////////////////////////////////////////////////////////////////
// JohnCMS                                                                    //
// Официальный сайт сайт проекта:      http://johncms.com                     //
// Дополнительный сайт поддержки:      http://gazenwagen.com                  //
////////////////////////////////////////////////////////////////////////////////
// JohnCMS core team:                                                         //
// Евгений Рябинин aka john77          john77@johncms.com                     //
// Олег Касьянов aka AlkatraZ          alkatraz@johncms.com                   //
//                                                                            //
// Информацию о версиях смотрите в прилагаемом файле version.txt              //
////////////////////////////////////////////////////////////////////////////////
*/

define('_IN_JOHNCMS', 1);
require_once('../incfiles/core.php');
$headmod = 'news';
$lng_news = core::load_lng('news');
function timer($var = '') {
	global $lng_news;
	if($var <= 0)
		return;
	if(86400 > $var) {
		if(3600 > $var) {
			if(60 > $var) {
				$time = $var;
				return $time . ' ' . $lng_news['sec'] . '.';
			} else if(60 <= $var && (60 * 2) > $var) {
				return $lng_news['one_minute'];
			}
			$hours = (60 - ceil((3600 - $var) / 60));
			return $hours . ' ' . $lng_news['minute'] . '.';
		} else if(3600 <= $var && (3600 * 2) > $var) {
			return $lng_news['one_hour'];
		}
		$days = (24 - ceil((86400 - $var) / 3600));
		return $days . ' ' . $lng_news['hour'] . '.';
	} else if(86400 <= $var && (86400 * 2) > $var) {
		return $lng_news['one_day'];
	}
	$days = ceil($var / 86400);
	return $days . ' дн.';
}
$mods = array (
'view',
'comments'
);
//Проверка выбора функции
if ($act && ($key = array_search($act, $mods)) !== false && file_exists('includes/' . $mods[$key] . '.php')) {
    require('includes/' . $mods[$key] . '.php');
} else {
	if($id) {
		$query = mysql_query("SELECT * FROM `cms_news_cat` WHERE `id`='$id' LIMIT 1;");
		if (mysql_num_rows($query)) {
			$req1 = mysql_fetch_assoc($query);
			
			$textl = $lng['news'] . ' | ' . htmlentities($req1['name'], ENT_QUOTES, 'UTF-8');
			require_once('../incfiles/head.php');
			echo '<div class="phdr"><h3><a href="./">' . $lng['news'] . '</a> | ' . htmlentities($req1['name'], ENT_QUOTES, 'UTF-8') . '</h3></div>';
			
			$total = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_news` WHERE `refid`='$id'".($rights < 7 ? " AND `time`<='" . time() . "'":"")), 0);
			if($total) {
				if ($total > $kmess) 
					echo '<div class="topmenu">' . functions::display_pagination('index.php?id=' . $id . '&amp;', $start, $total, $kmess) . '</div>';
				$req = mysql_query("SELECT `id`, `name`, `text`, `time` FROM `cms_news` WHERE `refid`='$id'".($rights < 7 ? " AND `time`<='" . time() . "'":"")."
				ORDER BY `time` DESC LIMIT "
							  . $start . "," . $kmess);
				$i = 1;
				while (($row = mysql_fetch_assoc($req)) !== false) {
					echo $i % 2 ? '<div class="list1">' : '<div class="list2">';
					if(file_exists('../files/news/small_news_' . $row['id'] . '.jpg') !== false) {
						echo '<table cellpadding="0" cellspacing="0" width="100%"><tr><td width="32">';
						echo '<img style="margin: 0 0 -3px 0;border: 0px;" src="../files/news/small_news_' . $row['id'] . '.jpg" alt="" width="32" height="32"/>&#160;';
						echo '</td><td>';
						echo '<a href="index.php?act=view&amp;id=' . $row['id'] . '">' . htmlentities($row['name'], ENT_QUOTES, 'UTF-8') . '</a> <br />(' . date('d.m.o / H:i', $row['time'] + $sdvigclock * 3600) . ')<br />';
						echo '</td></tr></table>';
					} else {
						echo '<a href="index.php?act=view&amp;id=' . $row['id'] . '">' . htmlentities($row['name'], ENT_QUOTES, 'UTF-8') . '</a> (' . date('d.m.o / H:i', $row['time'] + $sdvigclock * 3600) . ')<br />';
					}
					echo '<div class="sub"></div>';
					$text = $row['text'];
					if(mb_strlen($text) > 100) {
						$str = mb_substr($text, 0, 100);
						$text = mb_substr($str, 0, mb_strrpos($str, ' ')) . '...';
					}
					echo functions::checkout($text, 2, 1);
					if($row['time'] > time())
						echo '<div class="sub func">' . $lng_news['begin_show_through'] . ': ' . timer($row['time'] - time()) . '</div>';
					echo '</div>';
					++$i;
				}
				echo '<div class="phdr">' . $lng['total'] . ': ' . $total . '</div>';
				if ($total > $kmess) {
					echo '<div class="topmenu">' . functions::display_pagination('index.php?id=' . $id . '&amp;', $start, $total, $kmess) . '</div>';
					echo '<p><form action="index.php" method="get">
					<input type="hidden" name="id" value="' . $id . '"/>
					<input type="text" name="page" size="2"/>
					<input type="submit" value="' . $lng['to_page'] . ' &gt;&gt;"/></form></p>';
				}
			} else {
				echo '<div class="rmenu">' . $lng_news['news_no'] . '</div>';
			}
		} else {
			echo '<div class="rmenu">' . $lng_news['category_does_not_exist'] . '</div>';
		}
	} else {
		$textl = $lng['news'];
		require_once('../incfiles/head.php');
		echo '<div class="phdr"><h3>' . $lng['news'] . '</h3></div>';

		$total = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_news_cat`"), 0);
		if($total) {
			if ($total > $kmess) 
				echo '<div class="topmenu">' . functions::display_pagination('index.php?', $start, $total, $kmess) . '</div>';
			$req = mysql_query("SELECT `cms_news_cat`.`name`, `cms_news_cat`.`id`
			FROM `cms_news_cat`
			ORDER BY `cms_news_cat`.`realid` ASC LIMIT "  . $start . "," . $kmess);
			$i = 1;
			while (($row = mysql_fetch_assoc($req)) !== false) {
				$count = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_news` WHERE ".($rights < 7 ? "`time`<='" . time() . "' AND ":"")."`refid`='{$row['id']}'"), 0);
				echo $i % 2 ? '<div class="list1">' : '<div class="list2">';
				if(file_exists('../files/news/ico_cat_' . $row['id'] . '.jpg') !== false)
					echo '<img style="margin: 0 0 -3px 0;border: 0px;" src="../files/news/ico_cat_' . $row['id'] . '.jpg" alt="" width="16" height="16"/>&#160;';
				echo '<a href="index.php?id=' . $row['id'] . '">' . htmlentities($row['name'], ENT_QUOTES, 'UTF-8') . '</a> (' . $count . ')';
				echo '</div>';
				++$i;
			}
			echo '<div class="phdr">' . $lng['total'] . ': ' . $total . '</div>';
			if ($total > $kmess) {
				echo '<div class="topmenu">' . functions::display_pagination('index.php?', $start, $total, $kmess) . '</div>';
				echo '<p><form action="index.php" method="get">
				<input type="text" name="page" size="2"/>
				<input type="submit" value="' . $lng['to_page'] . ' &gt;&gt;"/></form></p>';
			}
		} else {
			echo '<div class="rmenu">' . $lng_news['not_nor_one_categories'] . '</div>';
		}
	}
	if($rights >= 7)
		echo '<div class="menu"><div class="func"><a href="../panel/index.php?act=news&amp;mod=news">' . $lng_news['add_news'] . '</a><br />
		<a href="../panel/index.php?act=news">' . $lng_news['management_news'] . '</a></div></div>';
}
require_once("../incfiles/end.php");